package com.moli.iov.down.command.mq.activemq;

import com.moli.iov.down.command.mq.activemq.handle.MessageHandler;
import com.moli.iov.down.model.dto.remoting.CommandDto;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  AcitveMQ消息监听器
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/27 09:44
 */
@Slf4j
@Component
public class ActiveMqListener {


    @Autowired
    private MessageHandler messageHandler;


    @JmsListener(destination = "${activemq.delay.queue}" + "-" + "${service.group}", containerFactory = "queueListenerFactory")
    public void onMessage(Message message) {
        try {
            if(message instanceof ObjectMessage){

                ObjectMessage objectMessage = (ObjectMessage) message;
                log.info("AcitveMQ消息监听器 接收到的消息：{}", objectMessage.getObject());
                CommandDto commandDto = (CommandDto)objectMessage.getObject();
                log.info("AcitveMQ消息监听器 接收到的消息：{}", commandDto);
                messageHandler.handle(commandDto);
            }else{
                log.info("ActiveMQ 收到的消息: {}", message);
            }
        } catch (JMSException e) {
            log.error("AcitveMQ消息监听器 onMessage 处理异常", e);
        }
    }


}
